// pages/backDetails/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: '',
    id: '',
    coverImage: "",//课程视频
    seeMinute: '',//试看时长
    currentTime: '',//视频当前时长
    allViedo: '',
    videoIndex: 0
  },
  onPay(e) {
    wx.navigateTo({
      url: '/pages/livePay/index?id=' + this.data.id,
    })
  },
  playClass(e) {
    let idx = e.currentTarget.id
    wx.showLoading({
      title: '请稍后...',
    })
    if (this.data.list.sub[idx].video_url != null) {
      setTimeout(() => {
        wx.hideLoading()
        this.setData({
          coverImage: this.data.list.sub[idx].video_url
        })
      }, 500)
    } else {
      setTimeout(() => {
        wx.hideLoading()
        this.setData({
          coverImage: this.data.list.sub[idx].reply.live_replay[0].media_url,
          allViedo: this.data.list.sub[idx].reply.live_replay
        })
      }, 500)
    }
  },
  // //播放进度变化时触发
  bindtimeupdate(e) {
    // let videoContext = wx.createVideoContext('myVideo', this)
    // videoContext.pause()
  },
  //播放到末尾触发
  bindended(e) {
    console.log(this.data.allViedo)
    let allVideo = this.data.allViedo
    let videoIndex = this.data.videoIndex
    console.log(videoIndex)
    console.log(allVideo.length - 1 > videoIndex)
    if (allVideo.length - 1 > videoIndex) {
      this.setData({
        videoIndex: videoIndex + 1
      })
      this.setData({
        coverImage: allVideo[this.data.videoIndex].media_url,
      })
      setTimeout(() => {
        let videoContext = wx.createVideoContext('myVideo', this)
        videoContext.play()
      }, 500)
      console.log(this.data.coverImage)
      console.log(this.data.videoIndex)
    } else {
      this.setData({
        videoIndex: 0
      })
      console.log(this.data.videoIndex)
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
    })
    this.getList()
  },
  getList() {
    let data = {
      method: 'order.courseinfo',
      course_id: this.data.id,
      token: wx.getStorageSync('token'),
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          list: res.data,
        })
        if (res.data.sub[0].video_url == null) {
          this.setData({
            coverImage: res.data.sub[0].reply.live_replay[0].media_url,
            allViedo: this.data.list.sub[0].reply.live_replay
          })
        } else {
          this.setData({
            coverImage: res.data.sub[0].video_url
          })
        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          })
        }, 500)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})